<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 通过依赖注入给子组件传递方法 -->
    <div id="app">
      <h2>依赖注入2 - 子传父 - {{ count }}</h2>
      <child></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp, computed } = Vue;
      const Child2 = {
        inject: ["fn2"],
        data() {
          return {
            count: 12345,
          };
        },
        template: `
          <div>这是child2子组件 - <button @click="fn2(this.count)">btn</button></div>
          
        `,
      };

      const Child = {
        data() {
          return {};
        },
        template: `
          <div>这是child子组件</div>
          <child2></child2>
        `,
        components: {
          Child2,
        },
      };

      const app = createApp({
        data() {
          return {
            count: 0,
          };
        },
        methods: {
          fn(n) {
            // console.log("fn", n);
            this.count = n;
          },
        },
        provide() {
          return {
            fn2: this.fn,
          };
        },
        components: {
          Child,
        },
      });

      app.config.unwrapInjectedRef = true;

      app.mount("#app");
    </script>
  </body>
</html>
